﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EIS.ClientUtility;
using EIS.ConfigViews;
using EIS.DatabaseHelper;
using EIS.Obj;
using EIS.ViewControl.Grid;

namespace EIS.Main
{
    public partial class MaintainLayout : Form
    {
        public MaintainLayout()
        {
            InitializeComponent();
        }

        public string layoutNameToAdd;
        private void btnDelete_Click(object sender, EventArgs e)
        {
            Janus.Windows.GridEX.GridEXRow[] drew = gridLayout.GetCheckedRows();
            for (int i = 0; i < drew.Length; i++)
            {
                drew[i].Delete();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DataSet ds = (DataSet)gridLayout.DataSource;
            ds.AcceptChanges();

            DataSet dsToSave = ds;

            if (dsToSave.Tables[0].Rows.Count == 0)
            {
                var dr = dsToSave.Tables[0].NewRow();
                dr["FunctionID"] = GlobalVariable.Instance.CurrentFunctionID;
                dsToSave.Tables[0].Rows.Add(dr);
                new DBHandler().DeleteRecords(EISConstant.ClientLayoutTableName, dsToSave.Tables[0]);
            }
            else
            {
                new DBHandler().UpdateRecords(EISConstant.ClientLayoutTableName, dsToSave.Tables[0]);
            }
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MaintainLayout_Load(object sender, EventArgs e)
        {
            var dbHandler = new DBHandler();
            this.tbCurrentFunction.Text = GlobalVariable.Instance.CurrentFunctionID;

            DataSet ds = dbHandler.SelectSimple(EISConstant.ClientLayoutTableName);
            var dsToShow = ds.Clone();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["FunctionID"].Equals(GlobalVariable.Instance.CurrentFunctionID))
                {
                    dsToShow.Tables[0].ImportRow(dr);
                }
            }

            gridLayout.DataSource = dsToShow;
            gridLayout.DataMember = dsToShow.Tables[0].TableName;
            gridLayout.SelectionMode = Janus.Windows.GridEX.SelectionMode.SingleSelection;
        }

        private void btnMarkDefault_Click(object sender, EventArgs e)
        {
            Janus.Windows.GridEX.GridEXRow[] drew = gridLayout.GetCheckedRows();
            if (drew.Length != 1)
            {
                ShowErrorMessageBox.Show("Please select exactly one layout.");
                return;
            }
            ((DataRowView) drew[0].DataRow).Row["Default"] = "Y";

             DataSet ds = (DataSet)gridLayout.DataSource;
             ds.AcceptChanges();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if ( dr["Layout"].ToString() != ((DataRowView)drew[0].DataRow).Row["Layout"].ToString() )
                {
                    dr["Default"] = "";
                }
            }
            gridLayout.Refetch();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            layoutNameToAdd = null;
            new AddLayoutPopup().ShowDialog(this);
            string layoutString =  "";
            if( layoutNameToAdd =="" ) return;
            if (layoutNameToAdd != "")
            {
                if (GlobalVariable.Instance.CurrentViewHandler != null)
                    layoutString = ((ViewGridDisplay) (GlobalVariable.Instance.CurrentViewHandler)).GetLayout();
            }
            
            DataSet ds = (DataSet)gridLayout.DataSource;
            DataRow dr = ds.Tables[0].NewRow();

            dr["LayoutXML"] = layoutString;
            dr["FunctionID"] = GlobalVariable.Instance.CurrentFunctionID;
            dr["Layout"] = layoutNameToAdd;
            dr["Default"] = "";

            ds.Tables[0].Rows.Add(dr);
            gridLayout.Refetch();
        }
    }
}
